/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testproject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author marco
 */
public class TCPservice implements Connection{
    private int port;
    private Service servant;
    ServerSocket ss = null;
    Socket socket = null;
    BufferedReader br;
    
    public TCPservice(Service servant, int port){
        this.servant = servant;
        this.port = port;
    }
    
    @Override
    public void run() {
        try {
            while(true){
             ss = new ServerSocket(port);
             socket = ss.accept();
             System.out.println("Waiting connections");
             br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             String event = br.readLine();
             servant.enqueue(event);
                    
             br.close();
             socket.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(TCPservice.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}
